<?php
class goods_num extends common
{
    public function __construct()
    {
        parent::__construct();
        $this->_table =  $this->prefix.'goods_num';
        $this->_pk = 'id';
    }
    
    /**
     * 入库操作通过商品数组添加
     * @param $goods 商品列表
     * @param $sid 仓库id
     * @return 
     */
    public function insert_by_goods($goods,$sid)
    {
        $sid = $sid?$sid:1;

        foreach ($goods as $key => $value) {
            if ($value['snumber'] > 0) {

                $good_num_arr = pdo_get('xn_erp_goods_num', array('gid' => $value['id'], 'sid' => $sid));

                $arr = array(
                    'gid' => $value['id'],
                    'sid' => $sid,
                    'num' => $value['snumber'],
                    'edittime' => TIMESTAMP,
                );
                if ($good_num_arr['id']) {
                    $arr['num'] = $good_num_arr['num'] + $value['snumber'];
                    $this->update_by_id($arr);
                } else {
                    $this->insert($arr);
                }
            }
        }

    }

}
